package 蓝桥杯算法;

import 二叉树.TreeNode;

import javax.lang.model.util.SimpleElementVisitor14;
import java.util.Deque;
import java.util.LinkedList;
import  java.util.Arrays;

public class day12 {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(
                new TreeNode(
                        new TreeNode(4),
                        2,
                        new TreeNode(6)
                ),
                1,
                new TreeNode(
                        new TreeNode(6),
                        2,
                        new TreeNode(4)
                )
        );
        day12 a=new day12();
        System.out.println(a.isSymmetric1(root));
        System.out.println(a.TreeNum2(root));
        int[] arr={12,10,15,20,6};
        int n=5;
        int[] record=new int[n];
        int[] result=new int[n];
        for(int i=0;i<n;i++){
            record[i]=arr[i];
        }
        //排序后标记
        Arrays.sort(record);
        if(n%2==1){
            int index=n/2;
            for(int i=0;i<n;i++){
                if(arr[i]<record[index]){
                    result[i]=record[index]-arr[i]+1;
                }
                else{
                    result[i]=0;
                }
            }
        }
        else{
            int index=n%2;
            for(int i=0;i<n;i++){
                if(arr[i]<record[index]){
                    result[i]=record[index]-arr[i];
                }
                else{
                    result[i]=0;
                }
            }
        }
        for(int i=0;i<n;i++){
            System.out.print(result[i]);
        }



    }
    public void loop(TreeNode root){
        if(root==null){
            return;
        }
        System.out.println(root.val);
        if(root.left!=null){
            loop(root.left);
        }

        if(root.right!=null){
            loop(root.right);
        }

    }
    public TreeNode interTrese(TreeNode root){//二叉树的反转
        if(root==null){
            return null;
        }
        interTrese(root.left);
        interTrese(root.right);
        swap(root.left,root.right);
        return root;
    }
    public void swap(TreeNode left,TreeNode right){
        TreeNode temp=left;
        left=right;
        right=temp;
    }
    public TreeNode interTrees2(TreeNode root){
        if(root==null){
            return null;
        }
        LinkedList<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        int len=queue.size();
        while(len>0){
            TreeNode temp=queue.pop();
            swap(root.left,root.right);
            if(temp.left!=null){
                queue.add(temp.left);
            }
            if(temp.right!=null){
                queue.add(root.right);
            }
        }
        return root;
    }
   public boolean isSymmetric1(TreeNode root){//对称二叉树
        return compare(root.left,root.right);
   }
   public boolean compare(TreeNode left,TreeNode right){
        if(left==null&&right==null){
            return true;
        }
        if(left!=null&&right==null){
            return false;
        }
        if(left==null&&right!=null){
            return false;
        }
        if(left.val!=right.val){
            return false;
        }
        Boolean compare1=compare(left.left,right.right);
        Boolean compare2=compare(left.right,right.left);
        return compare1&&compare2;
   }
public int maxDepth(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftDepth=maxDepth(root.left);
        int rightDepth=maxDepth(root.right);
        return Math.max(leftDepth,rightDepth);
  }
  public int maxDepth2(TreeNode root){//最大深度
        if(root==null){
            return 0;
        }
      Deque<TreeNode> deque=new LinkedList<>();
        deque.offer(root);
        int depth=0;
        while(!deque.isEmpty()){
            int size=deque.size();
            depth++;//遍历每一次
            for(int i=0;i<size;i++){
                TreeNode node=deque.poll();
                if(node.left!=null){
                    deque.offer(node.left);
                }
                if(node.right!=null){
                    deque.offer(node.right);
                }
            }
        }
        return depth;
  }
  public int minDepth(TreeNode root){//最小深度
        if(root==null){
            return 0;
        }
        int leftDepth=minDepth(root.left);
        int rightDepth=minDepth(root.right);
        if(root.left==null){//防止出现错误的最小值
            return rightDepth+1;
        }
        if(root.right==null){
            return  leftDepth+1;
        }
        return Math.min(leftDepth,rightDepth)+1;//返回上一层，结果+1
  }
  public int minDepth2(TreeNode root){
        if(root==null){
            return 0;
        }
        Deque<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        int depth=0;
        while(!queue.isEmpty()){
            int size=queue.size();
            depth++;
            for(int i=0;i<size;i++){
                TreeNode node=queue.pop();
                if(node.left==null&&node.right==null){
                    return depth;//存在其中一个节点左右都空的时候提前结束
                }
                if(node.left!=null){
                    queue.offer(node.left);
                }
                if(node.right!=null){
                    queue.offer(node.right);
                }
            }
        }
        return depth;
  }
  public int TreeNum(TreeNode node){//二叉树的数目
        if(node==null){
            return 0;
        }
        if(node.left==null&&node.right==null){
            return 1;
        }
        if(node.left==null&&node.right!=null){
            return TreeNum(node.right)+1;
        }
        if(node.left!=null&&node.right==null){
            return TreeNum(node.left)+1;
        }
        return TreeNum(node.left)+TreeNum(node.right)+1;
  }
  public int TreeNum2(TreeNode root){
        if(root==null){
            return 0;
        }
        Deque<TreeNode> deque=new LinkedList<>();
        deque.offer(root);
        int sum=0;
        while(!deque.isEmpty()){
            int size=deque.size();
           while (size-->0){
               TreeNode cur=deque.pop();
               sum++;
               if(cur.left!=null){
                   deque.offer(cur.left);
               }
               if(cur.right!=null){
                   deque.offer(cur.right);
               }
           }
        }
        return sum;
  }



}
